<?php

namespace app\api\controller;

use app\api\service\JuHe;
use app\common\controller\Api;
use think\Request;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = '';
    protected $noNeedRight = ['*'];
    public $juHe = '';

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->juHe = new JuHe();
    }

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * 全局miss路由
     */
    public function miss()
    {
        $this->error('非法访问', null, '404');
    }

    /**
     * 搜索
     */
    public function search()
    {
        $postData = json_decode(request()->getContent(), true);
        $postData['userInfo'] = $this->auth->getUserinfo();
        $postData['ip'] = request()->ip();

        // 数据验证
        $result = $this->validate($postData, 'Search');
        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }

        $data = $this->juHe->search($postData);
        if(!$data) $this->error($this->juHe->getError());

        $this->success('查询成功', $data);
    }

}
